package controller;

import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import main.model.Entity;
import main.model.Hierarchy;
import view.CreateHierarchyView;
import controller.editor.EditorController;

public class CreateHierarchyController extends Controller{

	private EditorController controller;
	private CreateHierarchyView view;
	private HashMap<String, Entity> map;
	private Entity parent;
	private Integer entitiesCount = 0;
	private Hierarchy h;

	public CreateHierarchyController(EditorController editorController) {
		this.controller = editorController;
		this.h = new Hierarchy();
		this.map = new HashMap<String, Entity>();
	}

	@Override
	public void displayView() {
		view = new CreateHierarchyView();
		view.setController(this);
		view.show();
	}

	public void handleButtonAddParentEntity() {
		if (this.parent == null){
			AddEntityToHierarchyController controller = new AddEntityToHierarchyController(this.controller.getCurrentDiagram(), this, Boolean.TRUE);
			controller.displayView();
		} else {
			JDialog dialogo = new JDialog(); 
			JOptionPane.showMessageDialog(dialogo,"No puede agregar otra clase como Padre, borre la actual y pruebe de nuevo", "Notificacion",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	public void handleButtonAddSubEntity() {
		AddEntityToHierarchyController controller = new AddEntityToHierarchyController(this.controller.getCurrentDiagram(), this, Boolean.FALSE);
		controller.displayView();
	}

	public void handleButtonTerminar() {
		if (!this.view.getName().trim().isEmpty() && this.parent != null && !this.map.isEmpty()){
			if (!this.controller.getProject().getComponentsNames().contains(this.view.getName())){
				this.h.setName(this.view.getName());
				this.h.setType(this.view.getTypeSelected());
				this.h.setParent(this.parent);
				for (String s : this.map.keySet()) {
					this.h.addEntity(this.map.get(s));
				}
				this.controller.getCurrentDiagram().addHierarchy(h);
				this.controller.getProject().addComponentName(h.getName());
				this.controller.notifyNewHierarchy(h);
			} else {
				JDialog dialogo = new JDialog(); 
				JOptionPane.showMessageDialog(dialogo,"Ya existe un componente con ese nombre", "Notificacion",
						JOptionPane.INFORMATION_MESSAGE);
			}
		} else {
			JDialog dialogo = new JDialog(); 
			JOptionPane.showMessageDialog(dialogo,"Complete Nombre, Padre e hijos de la jerarquia", "Notificacion",
					JOptionPane.INFORMATION_MESSAGE);	
		}
		view.hide();
	}

	@SuppressWarnings("rawtypes")
	private void deleteSubEntity(){
		DefaultTableModel model = (DefaultTableModel) this.view.getSubTableModel();
		int[] rows = this.view.getSubEntitiesTable().getSelectedRows();
		String entity = ((Vector) view.getSubTableModel().getDataVector()
				.elementAt(rows[0])).elementAt(0).toString();
		this.map.remove(entity);
		model.removeRow(rows[0]);
		this.entitiesCount--;
	}

	@SuppressWarnings({ "rawtypes", "unchecked", "serial", "unused" })
	public void addEntityToHierarchy(Entity e) {
		if (this.map.get(e.getName()) != null){
			JDialog dialogo = new JDialog(); 
			JOptionPane.showMessageDialog(dialogo,"Ya esta agregada esa entidad", "Notificacion",
					JOptionPane.INFORMATION_MESSAGE);
		} else {
			this.map.put(e.getName(), e);
			Vector rowData = new Vector(2);
			rowData.insertElementAt(e.getName(),0);
			rowData.insertElementAt("Borrar",1);
			this.view.getSubTableModel().addRow(rowData);
			Action delete = new AbstractAction()
			{
				public void actionPerformed(ActionEvent e)
				{
					deleteSubEntity();
				}

			};
			ButtonColumn deleteColumn = new ButtonColumn(view.getSubEntitiesTable(), delete, 1);
			this.entitiesCount++;
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked", "serial", "unused" })
	public void addParentToHierarchy(Entity e) {
		if (e != null){
			this.parent = e;
			Vector rowData = new Vector(2);
			rowData.insertElementAt(e.getName(),0);
			rowData.insertElementAt("Borrar",1);
			this.view.getEntitiesModel().addRow(rowData);
			Action delete = new AbstractAction()
			{
				public void actionPerformed(ActionEvent e)
				{
					deleteParentEntity();
				}

			};
			ButtonColumn deleteColumn = new ButtonColumn(view.getEntitiesTable(), delete, 1);
		}
	}

	public void deleteParentEntity(){
		DefaultTableModel model = (DefaultTableModel) this.view.getEntitiesModel();
		int[] rows = this.view.getEntitiesTable().getSelectedRows();
		model.removeRow(rows[0]);
		this.parent = null;
	}

}
